package main;


import entity.Phone;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Description
 * @Author:PrinceHan
 * @CreateTime:2021/11/02 19:36
 */
public class PhoneBook {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        String name;
        String tel;
        int op = 0;
        int index = -1;
        boolean isInPhoneBook = false;
        ArrayList<Phone> Phones = new ArrayList<>();
        //控制界面
        System.out.println("*******************电话本******************");
        System.out.println();
        System.out.println("1.添加*************2.删除*************3.修改");
        System.out.println();
        System.out.println("4.查询*************5.打印*************6.保存");
        System.out.println();
        System.out.println("*******************7.退出******************");
        while (true) {
            System.out.println("请输入你要执行的操作:");
            op = scanner.nextInt();
            switch (op) {
                case 1:
                    System.out.println("请输入你要添加的联系人的姓名:");
                    name = scanner.next();
                    System.out.println("请输入你要添加的联系人的号码:");
                    tel = scanner.next();
                    Phone p_tmp = new Phone(name, tel);
                    Phones.add(p_tmp);
                    break;
                case 2:
                    isInPhoneBook = false;
                    System.out.println("请输入你要删除的联系人的姓名:");
                    name = scanner.next();
                    for (int i = 0; i < Phones.size(); i++) {
                        if (name.equals(Phones.get(i).getName())) {
                            index = i;
                            isInPhoneBook = true;
                        }
                    }
                    if (isInPhoneBook) {
                        Phones.remove(index);
                        System.out.println("删除成功");
                    } else {
                        System.out.println("查无此人,删除失败");
                    }
                    break;
                case 3:
                    isInPhoneBook = false;
                    System.out.println("请输入你要修改的联系人的姓名:");
                    name = scanner.next();
                    for (int i = 0; i < Phones.size(); i++) {
                        if (name.equals(Phones.get(i).getName())) {
                            System.out.println("请输入你要修改的联系人的姓名:");
                            name = scanner.next();
                            System.out.println("请输入你要修改的联系人的电话:");
                            tel = scanner.next();
                            Phones.get(i).setName(name);
                            Phones.get(i).setPhonenum(tel);
                            isInPhoneBook = true;
                        }
                    }
                    if (isInPhoneBook) {
                        System.out.println("修改成功");
                    } else {
                        System.out.println("查无此人,修改失败");
                    }
                    break;
                case 4:
                    isInPhoneBook = false;
                    System.out.println("请输入你要查询的联系人的姓名:");
                    name = scanner.next();
                    for (int i = 0; i < Phones.size(); i++) {
                        if (name.equals(Phones.get(i).getName())) {
                            System.out.println((i + 1) + "\t\t" + Phones.get(i));
                            isInPhoneBook = true;
                        }
                    }
                    if (!isInPhoneBook) {
                        System.out.println("查无此人");
                    }
                    break;
                case 5:
                    File file = new File("PhoneBook.txt");

                    if (!file.exists()) {
                        file.createNewFile();
                    }

                    FileReader fr = new FileReader("PhoneBook.txt");
                    BufferedReader br = new BufferedReader(fr);
                    String tmp = null;
                    while ((tmp = br.readLine()) != null) {
                        String[] str = tmp.split(",");
                        System.out.println(str[0] + "," + str[1]);
                    }

                    fr.close();
                    br.close();
                case 6:
                    FileWriter fw = new FileWriter("PhoneBook.txt");

                    BufferedWriter bw = new BufferedWriter(fw);
                    if (Phones.size() == 0) {
                        System.out.println("当前电话本为空");
                    } else {
                        for (Phone phone : Phones) {
                            bw.write(phone.getName() + ',' + phone.getPhonenum());
                            bw.write('\n');
                        }
                        bw.flush();

                        fw.close();
                        bw.close();

                    }
                    System.out.println("保存成功");
                    break;
                case 7:
                    scanner.close();
                    System.out.println("退出电话本！！！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入1~7");
                    break;
            }
        }
    }
}
